package com.jueecy.controller;

import com.jueecy.pojo.User;
import com.jueecy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    //查询注册用户是否存在
    @GetMapping("/searchUser")
    public String searchUser(String email) {

        String flag = "";
        try {
            flag = userService.searchUser(email);
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }

    //新增用户
    @GetMapping("/addUser")
    Integer addUser(String email, String pwd) {
        User user = new User();
        user.setUser_email(email);
        user.setUser_pwd(pwd);
        return userService.addUser(user);
    }


    @GetMapping("/userLogin")
    public Boolean userLogin(String user_email, String user_pwd, HttpServletRequest request) {
        User user = new User();
        user.setUser_email(user_email);
        user.setUser_pwd(user_pwd);
        User login_user = new User();
        try {
            login_user = userService.userLogin(user);

        } catch (Exception e) {
            System.out.println("用户不存在！");
        }

        if (login_user != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", login_user);

            return true;
        } else {
            return false;
        }
    }


    @ResponseBody
    @GetMapping("/user")
    public User user(Integer user_id) {
        User user = userService.userPerson(user_id);
        System.out.println(user);
        return user;
    }


    @ResponseBody
    @RequestMapping("/userPerson")
    public Boolean userPerson(Integer user_id, String user_name, String user_email, String user_pwd, String user_address, String user_img, String user_chat) {


        //      把字符串中的反斜杠（\）替换成（/）
        /*
         *  replacement替换target
         */
        user_img = user_img.replaceAll("\\\\", "/");
        user_img = user_img.replace("D:/static", "");
        //      把字符串中的反斜杠（/）替换成（\）
        user_chat = user_chat.replaceAll("\\\\", "/");
        user_chat = user_chat.replace("D:/static", "");
        User u = new User();
        u.setUser_id(user_id);
        u.setUser_name(user_name);
        u.setUser_pwd(user_pwd);
        u.setUser_email(user_email);
        u.setUser_address(user_address);
        u.setUser_img(user_img);
        u.setUser_chat(user_chat);


        Integer tmp = userService.editPerson(u);
        if (tmp == 1) {
            return true;
        } else {
            return false;
        }
    }

    @GetMapping("/goodsOldOrderWeChat")
    public String goodsOldOrderWeChat(Integer user_id){
        return userService.goodsOldOrderWeChat(user_id);
    }


}
